<?php
declare (strict_types=1);

namespace app\common\model;

class Level extends Base
{
    protected $name = 'user_level';

    public static function upgrade($uid): bool|string
    {
        try {
            $list = Relation::where(['new_uid' => $uid])->order('algebra asc')->select();
            foreach ($list as $item) {
                $next = parent::where('id', '>', $item->user->level_id)
                    ->where('team_total', '<=', $item->user->team_total)
                    ->order('id desc')->find();
                if ($next) {
                    User::update(['level_id' => $next->id], ['id' => $item->uid]);
                }
            }

        } catch (\Exception $e) {

            return $e->getMessage();
        }

        return true;
    }
}